package com.coderman.infosys.auth.adapter.controller.role;

import com.coderman.infosys.auth.adapter.controller.BaseController;
import com.coderman.infosys.auth.adapter.convert.RoleConverter;
import com.coderman.infosys.auth.domain.enums.errors.RoleErrorEnum;
import com.coderman.infosys.plat.auth.common.dto.ResultDataDto;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coderman.infosys.auth.adapter.vo.role.request.CreateRoleRequestVO;
import com.coderman.infosys.auth.adapter.vo.role.RoleVO;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.coderman.infosys.auth.domain.bo.role.RoleBO;
import com.coderman.infosys.auth.domain.repository.UserRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


/**
* @Description:控制层
* @Author:
* @CreateTime:2022-01-04 15:27:39
* @version v1.0
*/
@RestController
public class RoleController extends BaseController {
	
	protected Logger logger = LoggerFactory.getLogger(RoleController.class);

	@Autowired
	private UserRoleRepository userRoleRepository;


	/**
	 *
	 * @Description 创建角色
	 * @param createRoleRequestVO
	 * @return RoleVO
	 */
	@RequestMapping(value = "/role/create")
	public ResultDataDto<RoleVO> createRole(@RequestBody CreateRoleRequestVO createRoleRequestVO){
        RoleBO roleBO = RoleConverter.INSTANCE.vo2bo(createRoleRequestVO);
		RoleConverter.INSTANCE.bo2VO(roleBO);
		return ResultDataDto.success(RoleConverter.INSTANCE.bo2VO(roleBO));
	}

	/**
	 *
	 * @Description 禁用角色
	 * @param roleCode
	 * @return Boolean
	 */
	@RequestMapping(value = "/role/disable")
	public ResultDataDto<Boolean> disable(@RequestParam(value = "roleCode", required = true)  String roleCode){
        boolean b = userRoleRepository.disableRole(roleCode);
		if(b){
			ResultDataDto.success(true);
		}
		return ResultDataDto.fail(RoleErrorEnum.ROLE_DISABLE_ERROR);
	}

	/**
	 *
	 * @Description 启用角色
	 * @param roleCode
	 * @return Boolean
	 */
	@RequestMapping(value = "/role/enable")
	public ResultDataDto<Boolean> enable(@RequestParam(value = "roleCode", required = true)  String roleCode){

		boolean b = userRoleRepository.enableRole(roleCode);
		if(b){
			ResultDataDto.success(true);
		}
		return ResultDataDto.fail(RoleErrorEnum.ROLE_ENABLE_ERROR);
	}

	/**
	 * @Description 角色关联用户
	 * @param userIdList
	 * @return Boolean
	 */
	@RequestMapping(value = "/role/relatetouser")
	public ResultDataDto<Boolean> relateToUser(@RequestParam(value = "userIds", required = true) List<Long> userIdList, @RequestParam(value = "roleCode", required = true) String roleCode){
		RoleBO roleBO = userRoleRepository.getByRoleCode(roleCode);
		//这个动作算是应用层逻辑，所以整体上下面的updateRole接口不是很合适
		roleBO.buildRoleUserRelation(userIdList);
		userRoleRepository.updateRole(roleBO);
		return ResultDataDto.success(true);
	}
}
